package cn.holy.app.xcddt.ui.activity.setting

import androidx.core.content.ContextCompat
import android.text.TextUtils
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.domain.UserBean
import cn.holy.app.xcddt.net.callback.LoadingCallback
import cn.holy.app.xcddt.repository.UserRepository
import cn.holy.app.xcddt.ui.activity.base.BaseActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog

import kotlinx.android.synthetic.main.activtiy_bind.*
import javax.inject.Inject

/**
 * Created by 69568 on 2018/3/5.
 */
@Route(path = "/app/setting/bind")
class BindActivity : BaseActivity() {
    @Inject
    lateinit var userRepo: UserRepository

    override fun initData() {
    }

    override fun initView() {
        App.appComponent.inject(this)
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        top_bar.setTitle("绑定手机").setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        top_bar.addLeftImageButton(R.mipmap.ic_back, R.id.action_close).setOnClickListener({ finish() })
        val user = userRepo.loadUser()
        if(user != null){
           et_phone.setText(user.phone)
        }
        btn_save.setOnClickListener {
            if (et_phone.text.toString().isEmpty()) {
                showTips("电话不能为空", QMUITipDialog.Builder.ICON_TYPE_INFO)
                return@setOnClickListener
            } else {
                val loading = getLoading("正在保存")
                loading.show()
                userRepo.savePhone(et_phone.text.toString(), object : LoadingCallback<UserBean> {
                    override fun onStart() {
                        hideLoading(loading)
                    }

                    override fun onError(msg: String) {
                        hideLoading(loading)

                    }

                    override fun onSuccess(t: UserBean) {
                        hideLoading(loading)
                        showTips("保存成功", QMUITipDialog.Builder.ICON_TYPE_SUCCESS, object : Callback {
                            override fun onSuccess() {
                                finish()
                            }
                        })

                    }

                    override fun onSuccess() {
                        hideLoading(loading)
                        showTips("保存成功", QMUITipDialog.Builder.ICON_TYPE_SUCCESS, object : Callback {
                            override fun onSuccess() {
                                finish()
                            }
                        })
                    }

                    override fun onCompleted() {
                    }

                })
            }

        }

    }

    override fun initLayout() {
        setContentView(R.layout.activtiy_bind)
    }

}